/******************************************************************************
 * @File name   :      UploadThirdPartyDocumentHandler.java
 *
 * @Author      :      Eric Zhang
 *
 * @Date        :      2011-10-25
 *
 * @Copyright Notice: 
 * Copyright (c) 2011 SGM, Inc. All  Rights Reserved.
 * This software is published under the terms of the SGM Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-10-25 下午03:20:46        Eric Zhang     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.logistic.view;

import java.util.Iterator;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import com.cap.bts.crmdp.dealer.logistic.dto.LogisticTemplateDto;
import com.cap.bts.framework.common.util.Excel2007DataValidationUtil;
import com.cap.bts.framework.common.view.documenthandler.AbstractExcel2007DocumentHandler;


/**
 * 上传第三方潜客模板
 */
@Component("uploadLogisticEventHandler")
public class UploadLogisticEventHandler extends
        AbstractExcel2007DocumentHandler {
	
	/**
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2011-10-19
	 * @see com.sgm.doss.framework.common.view.documenthandler.Excel2003DocumentHandler#doExportExcelDocument(java.util.Map, org.apache.poi.hssf.usermodel.HSSFWorkbook)
	 **/
	@Override
	public void doExportExcelDocument(Map<String, Object> model,
			XSSFWorkbook workbook) {
		String sheetName1="sheet1";
        String sheetName2="sheet2";
        XSSFSheet sheet1 = workbook.getSheet(sheetName1);
        XSSFSheet sheet2 = workbook.getSheet(sheetName2);
        LogisticTemplateDto quantityMktDto = (LogisticTemplateDto)model.get("LogisticTemplate.xlsx");
        if(quantityMktDto != null){
            //DealerNo
            Map<String, String> mapDealerNo=quantityMktDto.getMapDealerNo();
            setExcelTextValue(sheet2,mapDealerNo,0,0);
            Excel2007DataValidationUtil.setDataValidation(workbook,sheet1,sheetName2+"!$A$1:$A$"+mapDealerNo.size(),"mapDealerNo",1,3001,1,1);
            //CardName
            Map<String, String> mapCardName = quantityMktDto.getMapCardName();
            setExcelTextValue(sheet2,mapCardName,2,3);
            Excel2007DataValidationUtil.setDataValidation(workbook,sheet1,sheetName2+"!$C$1:$C$"+mapCardName.size(),"mapCardName",1,3001,7,7);

            Excel2007DataValidationUtil.hideExcelSheet(workbook, sheet2);
        }
	
	}
	/**
	 * 设置excel数据
	 * @Date        :      Oct 27, 2011
	 * @param sheet2
	 * @param map
	 * @param firstColumn
	 * @param secondColumn
	 */
	private void setExcelTextValue(XSSFSheet sheet2,Map<String,String> map,int firstColumn,int secondColumn){
        if(map!=null && (!map.isEmpty())){
            XSSFCell cellValue=null;
            XSSFCell cellKey=null;
            Iterator keys=map.keySet().iterator();
            int row=0;
            while(keys.hasNext()){
                cellValue = getCell(sheet2, row , firstColumn);
                cellKey = getCell(sheet2, row , secondColumn);
                String keyValue=String.valueOf(keys.next());
                setText(cellValue, map.get(keyValue));
                setText(cellKey, String.valueOf(keyValue));
                row++;
            }
        }
    }

}
